package com.ylr.blog.framework.manager;

import com.ylr.blog.common.constant.BlogConstant;
import com.ylr.blog.framework.service.BlogTagRelationService;
import lombok.AllArgsConstructor;
import org.apache.shardingsphere.infra.hint.HintManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2023-06-05 10:40:30
 * className: BlogTagRelationManager
 * version: 1.0
 * description:
 */
@Service("blogTagRelationManager")
@AllArgsConstructor
public class BlogTagRelationManager {

    private static final Logger log = LoggerFactory.getLogger(BlogTagRelationManager.class);

    private BlogTagRelationService blogTagRelationService;

    /**
     * 保存博客关联博客标签操作
     * @param blogId    博客主键
     * @param code      博客编号
     * @param tagIdList 博客标签主键列表
     */
    public void saveBlogTagRelation(Long blogId, String code, List<Long> tagIdList) {
        try (HintManager hintManager = HintManager.getInstance()) {
            // 设置分库分表的分片键
            hintManager.addDatabaseShardingValue(BlogConstant.BLOG_TAG_RELATION, code);
            hintManager.addTableShardingValue(BlogConstant.BLOG_TAG_RELATION, code);

            blogTagRelationService.saveBlogTagRelation(blogId, tagIdList);
        }
    }
}
